// pages/order-details/order-details.js
var util = require("../../utils/util.js");
import Notify from '../../dist/notify/notify';
var app = getApp();
Page({
  data: {
    patn: [{
      name: '0',
      value: '堂食',
      checked: 'true'
    }, {
      name: '1',
      value: '预订',
    }],
    methods: [{
      name: 1,
      value: '微信',
      checked: 'true'
    }, {
      name: 2,
      value: '余额',
    }, {
      name: 3,
      value: '奖金',
    }],
    tableNumber: ['1', '2', '3', '4', '5', '6'],
    personNumber: ['1人', '2人', '3人', '4人', '5人', '6人', '7人', '8人', '9人', '10人', '10人以上'],
    userId: '',
    shopId: '',
    shopName: '',
    shopCar: {}, //购物车（用户提交的菜单）
    shopOrder: {}, //商品菜单（格式化过的支付需要的参数）
    billPatn: '预订', //消费模式
    payMethod: 1, //支付方式  1微信 2余额 3奖金
    cost: 0, //消费金额
    tableIndex: 0, //桌号
    personIndex: 0, //人数
    billMark: '', //订单备注
    count: '', //菜品数量
    commitDate: '', //订单时间
    proportion: 0.97, //结算比例
    payAmount: '', //折后金额
    isPay: true,
    isPassword: false,
  },
  //格式化购物车信息、折后金额
  onLoad: function (options) {
    const that = this;
    const shopCar = JSON.parse(options.list);

    const payAmount = (options.cost * this.data.proportion).toFixed(1);

    console.log(payAmount);
    let shopOrder = [];
    for (let i = 0, len = shopCar.length; i < len; i++) {
      shopOrder[i] = {};
      shopOrder[i].foodid = shopCar[i].id;
      shopOrder[i].count = shopCar[i].orderCount;
    }

    that.setData({
      userId: app.globalData.userId,
      cost: options.cost,
      shopName: options.shopName,
      shopId: options.shopid,
      shopCar: shopCar,
      shopOrder: shopOrder,
      cost: parseFloat(options.cost),
      payAmount: payAmount
    })
  },
  //消费方式
  billPatn(e) {
    let that = this;
    that.setData({
      billPatn: e.detail.value
    })
    console.log(this.data.billPatn);
  },
  //付款方式
  payMethod(e) {
    let that = this;
    that.setData({
      payMethod: e.detail.value
    })
    console.log(e);
    //选择微信和余额享受折扣 奖金不享受折扣 如果选择微信支付方式为1 密码框隐藏...
    if (this.data.payMethod == '微信' || this.data.payMethod == '余额') {
      console.log('微信、余额支付' + this.data.payAmount);
      that.setData({
        isPay: true,
      })
      if(this.data.payMethod == '微信'){
        that.setData({
          payMethod: 1,
          isPassword:false
        })
      } else{
        that.setData({
          payMethod: 2,
          isPassword: true
        })
      }
    } else {
      console.log('奖金支付' + this.data.cost);
      that.setData({
        payMethod:3,
        isPay: false,
      })
    }
  },
  //桌号
  bindTableNumberChange(e) {
    this.setData({
      tableIndex: e.detail.value
    })
    console.log(this.data.tableIndex);
  },
  //人数
  bindPersonNumberChange(e) {
    this.setData({
      personIndex: e.detail.value
    })
  },
  //备注
  bindRemarks(e) {
    this.setData({
      billMark: e.detail.value
    })
  },
  //提交菜单
  submitMenu(e) {
    var that = this;
    wx.request({
      url: 'http://www.jiaertangshi.com:8089/bill/addBill/' + app.globalData.openId,
      data: {
        consumeAmount: this.data.cost,
        billPatn: this.data.billPatn,
        payMethod: this.data.payMethod,
        billingRatio: this.data.proportion,
        payAmount: this.data.payAmount,
        commitDate: util.showTime(),
        billMark: this.data.billMark,
        userid: this.data.userId,
        shopid: this.data.shopId,
        shopname: this.data.shopName,
        toders: this.data.shopOrder,
      },
      header: {
        "content-type": "application/json;charset=UTF-8" // 默认值
      },
      method: "POST",
      success: function(res) {
        if (!res.data.data){ //余额、奖金支付
          Notify({
            text: '付款成功！',
            duration: 1500,
            selector: '#van-notify',
            backgroundColor: '#21c7f4'
          });
        }else{
          that.doWxPay(res.data);//微信支付
        }
      },
      fail: function(err) {
        wx.showToast({
          icon: "none",
          title: '服务器异常，清稍候再试'
        })
      },
    });
  },
  doWxPay(param) {
    //小程序发起微信支付
    wx.requestPayment({
      timeStamp: param.data.timeStamp, //记住，这边的timeStamp一定要是字符串类型的，不然会报错
      nonceStr: param.data.nonceStr,
      package: param.data.package,
      signType: 'MD5',
      paySign: param.data.paySign,
      success: function(event) {
        // success
        console.log(event);
        wx.showToast({
          title: '支付成功',
          icon: 'success',
          duration: 2000
        });
      },
      fail: function(error) {
        // fail
        console.log("支付失败")
        console.log(error)
      },
      complete: function() {
        // complete
        console.log("pay complete")
      }
    });
  },
  back(e) {
    wx.navigateBack({
      delta: 1
    })
  }
})